using System;
using System.DHTML;
using Ext;

namespace SampleScripts.menu {
    public class ActionsScript {

        public static void main(Dictionary args) {
			ExtClass.onReady(new AnonymousDelegate(delegate() { new ActionsScript().init(); }));
        }

        public void init() {
			Action action = new Action(new ActionConfig()
				.text("Action 1")
				.handler(new AnonymousDelegate(delegate
					{
						MessageBox.alert("Click", "You clicked on \"Action 1\".");
					}))
				.iconCls("blist")
				.ToDictionary());

			Panel panel = new Panel(new PanelConfig()
				.title("Actions")
				.width(600)
				.height(300)
				.custom("bodyStyle", "padding: 10px;")	// lazy inline style

				.tbar(new object[] {
					action,								// <-- Add the action directly to a toolbar
					new Dictionary(
						"text", "Action Menu",
						"menu", new object[] {action})	// <-- Add the action directly to a menu
				})

				.items(new object[] {
					new Ext.Button(action)				// <-- Add the action as a button
				})

				.renderTo(ExtClass.getBody())
				.ToDictionary());

			// Buttons added to the toolbar of the Panel above
			// to test/demo doing group operations with an action
			panel.getTopToolbar().add("->", new Dictionary(
				"text", "Disable",
				"handler", new AnonymousDelegate(delegate {
					action.setDisabled(!(bool)Type.InvokeMethod(action, "isDisabled"));
					Type.InvokeMethod(this, "setText", (((bool)Type.InvokeMethod(action, "isDisabled")) ? "Enable" : "Disable"));
				})),
				new Dictionary(
				"text", "Change Text",
				"handler", new AnonymousDelegate(delegate {
					MessageBox.prompt("Enter Text", "Enter new text for Action 1:", new MessageBoxResponseDelegate( delegate (string btn, string text) {
						if (btn == "ok" && !String.IsNullOrEmpty(text)) {
							action.setText(text);
							action.setHandler(new AnonymousDelegate(delegate {
								MessageBox.alert("Click", "You clicked on \"" + text + "\"");
							}));
						}
					}));
				})),
				new Dictionary(
					"text", "Change Icon",
					"handler", new AnonymousDelegate(delegate {
						action.setIconClass(((string)Type.InvokeMethod(action, "getIconClass")) == "blist" ? "bmenu" : "blist");
					}))
			);
		}
    }
}
